package com.china.great.system.controller;

import com.china.great.system.common.CommonResultUtil;
import com.china.great.system.config.RedisConfig;
import com.china.great.system.entity.SystemLoginUser;
import com.china.great.system.entity.SystemUser;
import com.china.great.system.service.SystemUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Api("用户相关的api")
@RequestMapping("/api/v1")
public class SystemUserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SystemUserController.class);

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private RedisConfig redisConfig;

    @RequestMapping(value = "/getlist", method = RequestMethod.GET)
    @ApiOperation(value="获取列表")
    public Object getList(@RequestParam(value="pageSize", defaultValue="20") int pageSize,
    @RequestParam(value="pageNum", defaultValue="1") int pageNum){
        PageInfo list = systemUserService.getList(pageSize,pageNum);
        redisConfig.set("aa","bb");
        return CommonResultUtil.success(list);
    }

    @RequestMapping(value = "/getuser", method = RequestMethod.GET)
    @ApiOperation(value="获取用户")
    public Object getUser(){
        SystemLoginUser user = systemUserService.getUser("amos");
        LOGGER.info(user.toString());
        return CommonResultUtil.success(user);
    }

    @PostMapping(value = "/login")
    @ApiOperation(value = "登陆", notes = "登陆成功返回token,登陆之前请先注册账号")
    public Object login(@RequestBody SystemUser user){
        String response = systemUserService.login(user.getUsername(), user.getPassword());
        return CommonResultUtil.success(response);
    }


    @RequestMapping(value = "/insertUser", method = RequestMethod.POST)
    @ApiOperation(value="增加用户")
    public Object insert(@RequestBody SystemUser blog){
        int i = systemUserService.insert(blog);
        if (i > 0){
            return CommonResultUtil.success(i);
        }
        return CommonResultUtil.err(i);
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation(value="修改用户")
    public Object update(@RequestBody SystemUser blog){
        int i = systemUserService.update(blog);
        if (i > 0){
            return CommonResultUtil.success(i);
        }
        return CommonResultUtil.err(i);
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
    @ApiOperation(value="删除用户")
    public Object delete(@RequestParam int id){
        int i = systemUserService.delete(id);
        if (i > 0){
            return CommonResultUtil.success(i);
        }
        return CommonResultUtil.err(i);
    }

}
